<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 21:39
 */

namespace app\tools;

class Wx {


    public $wechat ;

    function __construct()
    {
        $this->wechat = config('wx');
    }

    function getUserInfo($openid){

        $access_token = MIniprogram::accessToken($this->wechat);

        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";

        $data = curl($url);

        /*$data = '{
                "subscribe": 1,
                "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
                "nickname": "Band",
                "sex": 1,
                "language": "zh_CN",
                "city": "广州",
                "province": "广东",
                "country": "中国",   "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
                "subscribe_time": 1382694957,
                "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL",
                "remark": "",
                "groupid": 0,
                "tagid_list":[128,2],
                "subscribe_scene": "ADD_SCENE_QR_CODE",
                "qr_scene": 98765,
                "qr_scene_str": ""
                }';*/
        $data = json_decode($data, true);

        if($data['errcode'] > 0){
            exit(fail());
        }

        return [
            "openid"=> $data['openid'],
            "unionid"=> $data['unionid'],
            "nickname"=> $data['nickname'],
            "avatar_url"=> $data['headimgurl'],
            "gender"=> $data['sex'],
            "language"=> $data['language'],
            "city"=> $data['city'],
            "province"=> $data['province'],
            "country"=> $data['country'],
        ];

    }


    function sendTemplate($param){

        $access_token = MIniprogram::accessToken($this->wechat);

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";


        $post = [
                "touser"=> $param['openid'],
                "template_id"=> $param['template_id']?:'KO7PzleBytwcD9tQ5bGixBdI7ewiSXIf_lM9az1JFWc',
                "data"=> $param['data'],
                "miniprogram"=> [
                    "appid"=> "wx9cf61f67ed5d5321",
                    "pagepath"=> $param['page']?:'pages/new_index/index'
                ]
                ];

        curl($url, json_encode($post));


    }

    function sendMsg($data)
    {
        $access_token = MIniprogram::accessToken($this->wechat);
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        $result = curl($url, json_encode($data, JSON_UNESCAPED_UNICODE));
//        file_put_contents('1.txt', $result);
    }

    function uploadImg($path)
    {
        $access_token = MIniprogram::accessToken($this->wechat);
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image";
        $data = curl($url, [
            'media'=> curl_file_create(realpath($path))
        ]);
        $data = json_decode($data, true);
        return $data['media_id'];
    }

    function createArticle($param)
    {
        $access_token = MIniprogram::accessToken($this->wechat);
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$access_token;
        $post = [
            "articles"=> [
                [
                    "title"=> $param['title'],
                    "thumb_media_id"=> $param['thumb'],
                    "author"=> $param['author'],
                    "digest"=> "",
                    "show_cover_pic"=> 0,
                    "content"=> $param['content'],
                    "content_source_url"=> "",
                    "need_open_comment"=> 0,
                    "only_fans_can_comment"=> 1

                ]
            ]
        ];

        $result = Curl::curl($url, json_encode($post, JSON_UNESCAPED_UNICODE));
//        echo $result;

        $result = json_decode($result, true);

        if(isset($result['errcode']) && $result['errcode'] > 0){
            throw new CustomException($result['errmsg']);
        }

        return $result['media_id'];
    }

}